package com.yuelao.rabbitmq.produce;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author FLY
 * @version v1.0
 * @project yuelao-server
 * @date 2017/9/15 14:30
 * @修改记录 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 */
@Slf4j
@Component
public class CallBackSend implements RabbitTemplate.ConfirmCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        rabbitTemplate.setConfirmCallback(this);
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        log.info("callbackSender UUID: " + correlationData.getId());
        rabbitTemplate.convertAndSend("exchange", "topic.messages", "测试mq回调", correlationData);
    }

    /**
     * 确认是否将消息发送到服务器，不能确定是否被消费
     *
     * @param correlationData
     * @param b
     * @param s
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            log.info("消息发送成功,UUID:{},flag:{}, msg:{}", correlationData.getId(), b, s);
        } else {
            log.info("消息发送失败,UUID:{},flag:{}, msg:{}", correlationData.getId(), b, s);
        }
    }
}
